
<template>
  <a-spin :loading="loading" style="width: 100vw">
    <div class="grid-one p-all-5 grid-gap-5">
      <a-typography-title :heading="6" style="">
        选择需要转化的字段
      </a-typography-title>
      <a-select
        allow-clear
        v-model="origin_filed"
        placeholder="选择需要转化的字段"
        :options="origin_filed_option"
        :field-names="{ value: 'id', label: 'name' }"
      ></a-select>
      <a-typography-title :heading="6"> 选择需要转化的字段 </a-typography-title>
      <a-select
        allow-clear
        v-model="target_filed"
        placeholder="选择目标字段"
        :options="target_filed_option"
        :field-names="{ value: 'id', label: 'name' }"
      ></a-select>
      <a-button
        :loading="buttonLoading"
        :disabled="!commitCan"
        type="primary"
        @click="exportVoid"
        >开始转换</a-button
      >
      <a-divider>温馨提示</a-divider>
      <a-typography-text type="warning" :size="10"
        >*为了数据安全,要转化的字段与目标字段不可相同</a-typography-text
      >
      <a-typography-text type="warning" :size="10"
        >*转化的字段必须是文本,数字,货币类型</a-typography-text
      >
    </div>
  </a-spin>
</template>

<script setup >
import { bitable } from "@lark-base-open/js-sdk";
import { ref, onMounted, computed, watch } from "vue";
import dayjs from "dayjs";
const buttonLoading = ref(false);
const fieldList = ref([]);
const origin_filed = ref("");
const target_filed = ref("");
const loading = ref(false);
const origin_filed_option = computed(() => {
  let arr = [];
  for (let item of fieldList.value) {
    if ([1, 2,99003].includes(item["type"])) {
      item["disabled"] = false;
      if (target_filed.value == item["id"]) {
        item["disabled"] = true;
      }
      arr.push(item);
    }
  }
  return arr;
});
const target_filed_option = computed(() => {
  let arr = [];
  for (let item of fieldList.value) {
    if ([1, 2,99003].includes(item["type"])) {
      item["disabled"] = false;
      if (origin_filed.value == item["id"]) {
        item["disabled"] = true;
      }
      arr.push(item);
    }
  }
  return arr;
});
const commitCan = computed(() => {
  if (origin_filed.value && target_filed.value) {
    return true;
  }
  return false;
});
let table = "";
onMounted(async () => {
  initData();
});

async function initData() {
  loading.value = true;
  table = await bitable.base.getActiveTable();
  table.onFieldAdd((event) => {
    getAllField();
  });
  getAllField();
}
async function getAllField() {
  loading.value = true;
  const fieldMetaList = await table.getFieldMetaList();
  fieldList.value = fieldMetaList;
  loading.value = false;
  console.log("你好", fieldList.value);
}

// 导出word
async function exportVoid() {
  buttonLoading.value = true;
  const recordList = await table.getRecordList();
  const view = await table.getActiveView();
  const recordIdList = await view.getVisibleRecordIdList();
  let newDataArr = [];
  for (const record of recordList) {
    if (!recordIdList.includes(record.id)) {
      continue;
    }
    // const cellList = await record.getCellList();
    const cell = await record.getCellByField(origin_filed.value);
    console.log(cell);
    const value = cell.val;
    if (!value) {
      continue;
    }
    let valueStr = "";
    if (Array.isArray(value)) {
      valueStr = value.map((a) => a["text"]).join("");
    } else if (typeof value == "object") {
      valueStr = value["text"];
    } else {
      valueStr = value;
    }
    const numStr = numberToChineseUppercase(valueStr);
    if (numStr) {
      const dic = {
        recordId: record.id,
        fields: {
          [target_filed.value]: numStr,
        },
      };
      newDataArr.push(dic);
    }
  }
  await table.setRecords(newDataArr);
  buttonLoading.value = false;
}
function numberToChineseUppercase(num) {
  const str = parseInt(num).toString();
  if (!str) {
    return "";
  }
  let len = str.length;
  let numArray = [];
  for (let i = 0; i < len; i++) {
    let n = str.charAt(i);
    numArray.push(n);
  }
  const chineseNumbers = [
    "零",
    "壹",
    "贰",
    "叁",
    "肆",
    "伍",
    "陆",
    "柒",
    "捌",
    "玖",
  ];
  const units = ["", "拾", "佰", "仟", "万", "亿"];
  let result = "";
  let zeroFlag = false;
  for (let i = 0; i < numArray.length; i++) {
    const num = numArray[i];
    const unit = units[numArray.length - i - 1];

    if (num === 0) {
      zeroFlag = true;
    } else {
      if (zeroFlag) {
        result += chineseNumbers[0];
        zeroFlag = false;
      }
      result += chineseNumbers[num] + unit;
    }
  }

  return result;
}
</script>



<style >
</style>
